package com.sayice.coursetable.logic.utils;

import com.sayice.coursetable.logic.model.Course;

import java.util.Arrays;
import java.util.List;

import java8.util.stream.Collectors;
import java8.util.stream.StreamSupport;

/**
 * 课程工具类
 */
public abstract class CourseUtils {
    /**
     * 筛选出某周的课程
     *
     * @param courses 课程
     * @param week    周次
     * @return 返回在该周开课的课程
     */
    public static List<Course> findForWeek(List<Course> courses, int week) {
        return StreamSupport.stream(courses).filter(course -> {
            String[] ranges = course.weeks.split(",");
            for (String range : ranges) {
                int start;
                int end = 0;
                String[] numbers = StreamSupport.stream(Arrays.asList(
                        range.replaceAll("\\D+", ",").split(",")))
                        .filter(s -> !"".equals(s))
                        .toArray(String[]::new);
                start = Integer.parseInt(numbers[0]);
                if (numbers.length == 2) {
                    end = Integer.parseInt(numbers[1]);
                }
                if (week == start && end == 0) {
                    return true;
                }
                if (week >= start && week <= end) {
                    return true;
                }
            }
            return false;
        }).collect(Collectors.toList());
    }
}
